package com.impl.baseUtil;


import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Data
@ApiModel(value = "全局统一返回结果类")
public class AjaxResult {

    @ApiModelProperty(value = "是否成功")
    private boolean success;

    @ApiModelProperty(value = "返回码")
    private Integer code;

    @ApiModelProperty(value = "返回信息")
    private String message;

    @ApiModelProperty(value = "返回的数据")
    private Map<String, Object> data = new ConcurrentHashMap<>();

    public AjaxResult() {
    }


    public static AjaxResult ok() {
        AjaxResult r = new AjaxResult();

        r.setSuccess(ResultCodeEnum.SUCCESS.getSuccess());
        r.setCode(ResultCodeEnum.SUCCESS.getCode());
        r.setMessage(ResultCodeEnum.SUCCESS.getMessage());
        return r;
    }

    public static AjaxResult error() {
        AjaxResult r = new AjaxResult();
        r.setSuccess(ResultCodeEnum.UNKNOWN_REASON.getSuccess());
        r.setCode(ResultCodeEnum.UNKNOWN_REASON.getCode());
        r.setMessage(ResultCodeEnum.UNKNOWN_REASON.getMessage());
        return r;
    }

    public static AjaxResult resultError(ResultCodeEnum resultCodeEnum) {

        AjaxResult r = new AjaxResult();
        r.setSuccess(resultCodeEnum.getSuccess());
        r.setCode(resultCodeEnum.getCode());
        r.setMessage(resultCodeEnum.getMessage());

        return r;
    }


    public AjaxResult success(Boolean success) {
        this.setSuccess(success);
        return this;
    }

    public AjaxResult code(Integer code) {

        this.setCode(code);
        return this;
    }

    public AjaxResult message(String message) {

        this.setMessage(message);
        return this;
    }


    public AjaxResult data(String key, Object value) {
        this.data.put(key, value);
        return this;
    }

    public AjaxResult dataMap(Map<String, Object> map) {

        this.setData(map);
        return this;
    }

}
